<?php
namespace Home\Controller;
use Think\Controller;
class MrysController extends Controller {
    public function index(){
        $imei=cookie('imei');//生成唯一标识imei
        $uid=cookie('uid');
        if(empty($imei)){$imei=date('Ymd').mt_rand(10000,99999).date('His');cookie('imei',$imei);}
        if(empty($uid)){cookie('uid',$imei);}

        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            getToken();
            getTicket();

            $wzurl=wxJsdkData();

            if(!empty($_REQUEST['code'])){
                cookie('code',$_REQUEST['code'],7100);
            }
            getCode($wzurl);
            getOopenid();
            cookie('uid',cookie('openid'));
        }
        $imei=cookie('openid');if(cookie('openid')==''){$imei=cookie('imei');}
        $arrdata=M()->query("select * from tb_meiri_user where appuserid='{$imei}' limit 1");
        $this->assign('arrdata',$arrdata[0]);

        if($_REQUEST['data']!=1&&$arrdata[0]){
            $this->jieguoye();exit;
        }

        $this->display("Mrys/index");
    }
    public function jieguoye(){
        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            getToken();
            getTicket();
            $noncestr=noncestr(15);
            $timestamp=time();
            $ticket= S('ticket');
            $wzurl=wxJsdkData();
            $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

            $this->assign('appid',cookie('APPID'));
            $this->assign('timestamp',$timestamp);
            $this->assign('noncestr',$noncestr);
            $this->assign('signature',$signature);
            //cookie('JPwxfxUrl','http://hy.yixueqm.com/zhiming/index.php/Home-Bzjp-index');
            wxShareUrl('MR','Mrys');//分享地址获取
            if(!empty($_REQUEST['code'])){
                cookie('code',$_REQUEST['code'],7100);
            }
            getCode($wzurl);
            getOopenid();
        }

        if($_REQUEST['birthday']==''){
            $birthday='0-2018-08-08-8';
        }else{
            $birthday=$_REQUEST['birthday'];
        }
        if($_REQUEST['username']){//返回操作
            cookie('znickname',I("request.username"));
            cookie('zymd',mb_substr($birthday,2,10));
            cookie('zhour',mb_substr($birthday,13,2));
            cookie('datetype',mb_substr($birthday,0,1));
            cookie('zsex',$_REQUEST['sex']);
            cookie('phone',$_REQUEST['phone']);
        }

        //创建订单
        $hour=cookie('zhour');
        $znickname=cookie('znickname');if($znickname=='')$znickname='匿名';
        $zsex=cookie('zsex');if($zsex=='')$zsex=1;
        $imei=cookie('openid');

//        if(cookie('uid')==''){$imei=cookie('imei');}
        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $phone=cookie('phone');

        if($_REQUEST['username']){
            $arrdata=M()->query("select id from tb_meiri_user where appuserid='{$imei}' limit 1");
            if(empty($arrdata)){
                M()->query("insert into tb_meiri_user (username,sex,datetype,year,month,day,hour,phone,appuserid)values(
                                         '{$znickname}','{$zsex}',0,'{$y}','{$m}','{$d}','{$hour}','{$phone}','{$imei}')");
            }else{
                //更新订单
                M()->query("update tb_meiri_user set username='{$znickname}',phone='{$phone}',datetype=0,year={$y},month={$m},day={$d},sex={$zsex} where id='{$arrdata[0]['id']}'");
            }
        }else{
            $arrdata=M()->query("select * from tb_meiri_user where appuserid='{$imei}' limit 1");
            $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
            cookie('zymd',$zymd,604800);
            $zhour=$arrdata[0]['hour'];
            cookie('znickname',$arrdata[0]['username']);
            cookie('zsex',$arrdata[0]['sex']);
            cookie('zhour',$arrdata[0]['hour']);
        }

        if(cookie('zsex')==1){$zsex='男';}else{$zsex='女';}
        zwmp(cookie('zymd'), cookie('zhour'),$zsex,'sizhu');
        $sizhu=I('globals.sizhu');
        $this->assign('sizhu',$sizhu);//Z四柱

        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $zyinli=yinli($y,$m,$d);
        cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时');//阳历
        cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2]." ".mb_substr($sizhu[3],1,1,'utf-8')."时");//阴历


        $dateN=date("Y");
        $dateY=date("m");
        $dateR=date("d");
        $riNum=($y+$m+$d+$dateN+$dateY+$dateR)%5;
        if($riNum==0)$riNum=5;
        $judge=($m+$d)%2;
        if($judge==1){$ymdNum='日';}else{$ymdNum='月';}
//        $yueNum=($y+$m)%5;
//        if($yueNum==0)$yueNum=5;
//        $nianNum=($y+$d)%5;
//        if($nianNum==0)$nianNum=5;

        $riArr=M()->query("select * from tb_meiri_yunshi where ymd='{$ymdNum}' and number='{$riNum}'");
//        $yueArr=M()->query("select * from tb_meiri_yunshi where ymd='月' and number='{$yueNum}'");
//        $nianArr=M()->query("select * from tb_meiri_yunshi where ymd='年' and number='{$nianNum}'");
        $this->assign('riArr',$riArr);
//        $this->assign('yueArr',$yueArr);
//        $this->assign('nianArr',$nianArr);

            zwmp(date("Y-m-d"),cookie('zhour'),$zsex,'zw');
            $this->assign('shengxiao',I('globals.shengxiao'));//生肖
            $this->assign('arryysz',I('globals.arryysz'));//四柱阴阳
            $this->assign('sizhu',I('globals.sizhu'));//Z四柱
            $this->assign('sizhuny',I('globals.sizhuny'));//四柱纳音
        $this->assign('date',date("Y-m-d"));

        $this->display("Mrys/day_yunshi");
    }
}